import Vue from 'vue'
import Clipboard from 'clipboard'

function clipboardSuccess(tipText = '复制成功') {
  Vue.prototype.$Message.success({
    content: tipText,
    duration: 1
  })
}

function clipboardError() {
  Vue.prototype.$Message.error({
    content: '复制失败'
  })
}

export default function handleClipboard(text, event, tipText) {
  const clipboard = new Clipboard(event.target, {
    text: () => text
  });
  clipboard.on('success', () => {
    clipboardSuccess(tipText);
    clipboard.destroy()
  });
  clipboard.on('error', () => {
    clipboardError();
    clipboard.destroy()
  });
  clipboard.onClick(event)
}